package application.gui.adminWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;

import javax.print.PrintException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

import application.DatabaseBackupModule.BackupManager;
import application.LoginAndAdministrationModule.Admin;
import application.LoginAndAdministrationModule.PersonData;
import application.PrintingModule.Printing;
import application.generateReports.DateReport;
import application.gui.main.startWindow.Homepage;
import application.gui.workerWindow.WorkerHomePage;

import com.itextpdf.text.DocumentException;
import com.toedter.calendar.JDateChooser;

public class AdminHomePage extends WorkerHomePage {

	public AdminHomePage(PersonData loggedUser, Homepage f) {
		super(loggedUser, f);
		this.admin = new Admin(loggedUser);
		createMenuPanel();
		setListeners();
		adminPanel = new AdminPanel(AdminHomePage.this, getDb(), admin,
				AdminPanel.WORKER_PANEL);
		add(adminPanel);
		adminPanel.setBounds(200, 120, 600, 480);
		adminPanel.setVisible(false);
		createReports();
		createBackupPanel();
	}

	private void createMenuPanel() {
		showWorkers = new JButton("Pracownicy");
		addComponent(this.getMenuPanel(), showWorkers, 0, 310, 200, 30);
		reports = new JButton("Raporty");
		addComponent(this.getMenuPanel(), reports, 0, 340, 200, 30);
		backup = new JButton("Kopia zapasowa");
		addComponent(getMenuPanel(), backup, 0, 370, 200, 30);
		deleteParcel = new JButton("Usuwanie paczek");
		addComponent(getMenuPanel(), deleteParcel, 0, 400, 200, 30);
		
	}

	private void setListeners() {
		showWorkers.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				remove(adminPanel);
				repaint();
				validate();
				getMainPanel().setVisible(false);
				getNewParcelPanel().setVisible(false);
				getShowParcelPanel().setVisible(false);
				getDataPanel().setVisible(false);
				getDeliveryPanel().setVisible(false);
				getStatusPanel().setVisible(false);
				backupPanel.setVisible(false);
				getClientPanel().setVisible(false);
				reportPanel.setVisible(false);
				adminPanel = new AdminPanel(AdminHomePage.this, getDb(), admin,
						AdminPanel.WORKER_PANEL);
				add(adminPanel);
				adminPanel.setBounds(200, 120, 600, 480);
				repaint();
				validate();
			}
		});
		deleteParcel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				remove(adminPanel);
				repaint();
				validate();
				getMainPanel().setVisible(false);
				getNewParcelPanel().setVisible(false);
				getShowParcelPanel().setVisible(false);
				getDataPanel().setVisible(false);
				getDeliveryPanel().setVisible(false);
				getStatusPanel().setVisible(false);
				getClientPanel().setVisible(false);
				backupPanel.setVisible(false);
				reportPanel.setVisible(false);
				adminPanel = new AdminPanel(AdminHomePage.this, getDb(), admin,
						AdminPanel.PARCEL_PANEL);
				add(adminPanel);
				adminPanel.setBounds(200, 120, 600, 480);
				repaint();
				validate();
			}
		});
		reports.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				remove(adminPanel);
				repaint();
				validate();
				getMainPanel().setVisible(false);
				getNewParcelPanel().setVisible(false);
				getShowParcelPanel().setVisible(false);
				getDataPanel().setVisible(false);
				getDeliveryPanel().setVisible(false);
				getStatusPanel().setVisible(false);
				getClientPanel().setVisible(false);
				reportPanel.setVisible(true);
				backupPanel.setVisible(false);
			}
		});
		backup.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				remove(adminPanel);
				repaint();
				validate();
				getMainPanel().setVisible(false);
				getNewParcelPanel().setVisible(false);
				getShowParcelPanel().setVisible(false);
				getDataPanel().setVisible(false);
				getDeliveryPanel().setVisible(false);
				getStatusPanel().setVisible(false);
				getClientPanel().setVisible(false);
				reportPanel.setVisible(false);
				backupPanel.setVisible(true);
			}
		});
		getAddParcel().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				hidePanels();
			}
		});
		getShowMyParcel().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				hidePanels();
			}
		});

		getParcelHistory().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				hidePanels();
			}
		});

		getCheckDeliveryStatusButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				hidePanels();

			}
		});
		
		getChangeData().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				hidePanels();
			}
		});
getChangeParcelStatus().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				hidePanels();
			}
		});
		getClientList().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				hidePanels();
			}
		});
		
		
	}
	
	private void hidePanels(){
		reportPanel.setVisible(false);
		backupPanel.setVisible(false);
		adminPanel.setVisible(false);
	}
	public JButton getDeleteParcel() {
		return deleteParcel;
	}


	public JButton getShowWorkers() {
		return showWorkers;
	}

	private void createReports(){
		reportPanel = new JPanel();
		add(reportPanel);
		reportPanel.setBounds(200, 120, 600, 480);
		reportPanel.setLayout(null);
		reportPanel.setVisible(false);
		dateFrom = new JDateChooser();
		dateTo = new JDateChooser();
		dailyReport = new JButton("Dzienny raport");
		dateReport = new JButton("Generuj");
		reportPanel.add(dailyReport);
		reportPanel.add(dateFrom);
		reportPanel.add(dateTo);
		reportPanel.add(dateReport);
		dailyReport.setBounds(250, 50, 150, 50);
		dateFrom.setBounds(50, 150, 150, 30);
		dateTo.setBounds(230, 150, 150, 30);
		dateReport.setBounds(410, 150, 150, 30);
		from = new JLabel("Od:");
		to = new JLabel("Do:");
		reportPanel.add(from);
		reportPanel.add(to);
		from.setBounds(30, 150, 30, 30);
		to.setBounds(210, 150, 30, 30);
		dailyReport.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				printReport(1);
					
				
			}
		});
		dateReport.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				printReport(2);
			}
		});
	
	}

private void printReport(int type){
	Printing print = new Printing();
	try {
		JFileChooser chooser = new JFileChooser(".");
		FileNameExtensionFilter filter = new FileNameExtensionFilter(
		        "pdf", "pdf");
		    chooser.setFileFilter(filter);
		int returnVal = chooser.showSaveDialog(AdminHomePage.this);
		if(returnVal == JFileChooser.APPROVE_OPTION) {
		      String filename = chooser.getSelectedFile().getAbsolutePath();
		      if (type==1)
		      print.generateDailyReport(filename+".pdf", new DateReport("inzynier_admin", "admin"));
		      else {
		    	  DateFormat df = DateFormat.getDateInstance();
		    	  print.generatePeriodicReport(filename+".pdf", new DateReport("inzynier_admin", "admin", df.format(dateFrom.getDate()), df.format(dateTo.getDate())));
		      }
		    	  int res = JOptionPane.showConfirmDialog(AdminHomePage.this, "Raport zapisano. Czy chcesz go wydrukować", "OK", JOptionPane.INFORMATION_MESSAGE);
		    	  if (res==0)
		    		  print.printDocument(filename+".pdf");
		} }catch (FileNotFoundException e1) {
			
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			
			e1.printStackTrace();
		} catch (DocumentException e1) {
			
			e1.printStackTrace();
		} catch (SQLException e1) {
			
			e1.printStackTrace();
		} catch (PrintException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (PrinterException e) {
			
			e.printStackTrace();
		}
		}

private void createBackupPanel(){
	backupPanel = new JPanel();
	backupPanel.setLayout(null);
	backupPanel.setVisible(false);
	add(backupPanel);
	backupPanel.setBounds(200, 120, 600, 480);
	createBackup = new JButton("Stwórz kopię");
	loadBackup = new JButton("Przywróć");
	listBackup = new JComboBox();
	backupPanel.add(createBackup);
	backupPanel.add(loadBackup);
	backupPanel.add(listBackup);
	infoBackup = new JLabel("Lista kopii");
	backupPanel.add(infoBackup);
	createBackup.setBounds(225, 50, 150, 40);
	loadBackup.setBounds(225, 100, 150, 40);
	infoBackup.setBounds(100, 160, 60, 30);
	listBackup.setBounds(170, 160, 100, 30);
	infoBackup.setVisible(false);
	backupManager = new BackupManager();
	for (String s: backupManager.getBackupList()){
		listBackup.addItem(s);
	}
	createBackup.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			try {
				backupManager.createBackup();
				listBackup.removeAllItems();
				for (String s: backupManager.getBackupList()){
					listBackup.addItem(s);
				}
				repaint();
				validate();
				
			} catch (IOException e1) {
				
				e1.printStackTrace();
			}
		}
	});
	loadBackup.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			try {
				backupManager.useBackup(String.valueOf(listBackup.getSelectedItem()));
				dispose();
			} catch (IOException e1) {
				
				e1.printStackTrace();
			}
		}
	});
}


	private JButton showWorkers;
	private JButton reports;
	private JButton backup;
	private JButton deleteParcel;
	private Admin admin;
	private AdminPanel adminPanel;
	
	//Raporty
	private JDateChooser dateFrom;
	private JDateChooser dateTo;
	private JPanel reportPanel;
	private JButton dailyReport;
	private JButton dateReport;
	private JLabel from;
	private JLabel to;
	
	//Backup
	private JPanel backupPanel;
	private JButton createBackup;
	private JButton loadBackup;
	private JComboBox listBackup;
	private BackupManager backupManager;
	private JLabel infoBackup;
}
